<?php

namespace app\controllers\h5\uni;

use app\commission\Order;
use app\controllers\h5\base\H5Controller;
use app\models\AdminSupplier;
use app\models\product\Product;
use app\models\SupplierCategory;
use app\models\system\SetFlow;
use app\models\system\SetSwiper;
use app\models\order\Order as MerchantOrder;

class OfflineController extends H5Controller
{
    public function actionIndex()
    {
        // 商家轮播图
        $swiper = SetSwiper::find()
            ->where(['status' => SetSwiper::$statusMapping['OK']])
            ->andWhere(['category_id' => 11])
            ->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['image'])
            ->asArray()
            ->all();

        // 商家列表
        $merchant = AdminSupplier::find()
            ->where(['status' => 1])
            ->with(['category', 'descImage', 'descImage.image'])
            ->asArray()
            ->all();

        // 商家分类
        // 如果没有产品则不显示
        $ids = Product::find()
            ->where(['status' => Product::$statusMapping['OK']])
            ->andWhere(['>=', 'supplier_id', 1])
            ->select(['category_id'])
            ->column();

        $category = SupplierCategory::find()
            ->where(['status' => 1])
            ->andWhere(['id' => $ids])
            ->with(['image.image'])
            ->asArray()
            ->all();

        return $this->successEncode([
            'swiper' => $swiper,
            'merchant' => $merchant,
            'category' => $category,
        ]);
    }

    public function actionProduct()
    {
        $address_id = $this->post('Address_id');
        // var_dump($address_id);exit;
        if (!$address_id) {
            // return $this->successEncode([
            //     $this->list => [],
            //     $this->totalCount => 0,
            //     $this->totalPage => 0,
            //     $this->currentPage => 1
            // ]);

            // 如果没有选地区
            // 则默认全部显示
        }


        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        // 地区
        $supplier_ids = AdminSupplier::find()
            ->where(['status' => 1])
            ->andWhere(['address_id' => $address_id])
            ->select(['id'])
            ->column();

        // var_dump($supplier_ids);exit;

        // 产品分区
        $area_id = 3;

        // 审核处理一下
        $set = SetFlow::getLastOne();
        if ($set->wx == 1) {
            $query = Product::find()
                ->where(['status' => Product::$statusMapping['OK']]);
        } else {
            $query = Product::find()
                ->where(['status' => Product::$statusMapping['OK']])
            // ->andWhere(['area_id' => $area_id])
            ->andWhere(['supplier_id' => $supplier_ids]);
        }

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['mainImage'])
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);

    }

    // 商家分类商品
    public function actionSupplierCategoryProduct()
    {
        $category_id = $this->post('id');


        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = Product::find()
            ->where(['status' => Product::$statusMapping['OK']])
            // 这里的分类共用了, 所以要是供应商产品
            ->andWhere(['category_id' => $category_id])
            ->andWhere(['>=', 'supplier_id', 1]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['mainImage'])
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);


    }

    // 已完成核销的商家订单
    public function actionMerchantOrder()
    {
        $merchant_id = $this->post('merchant_id');

        // var_dump($merchant_id);exit;


        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $query = MerchantOrder::find()
            ->where(['status' => MerchantOrder::$statusMapping['FINISH']])
            ->andWhere(['supplier_id' => $merchant_id]);

        $totalCount = $query->count();
        $totalPage = ceil($totalCount / $pageSize);

        $list = $query->orderBy(['update_time' => SORT_DESC])
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->with(['item', 'item.product', 'item.product.mainImage'])
            ->asArray()
            ->all();

        return $this->successEncode([
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    // 扫码核销
    public function actionMerchantScan()
    {
        $id = $this->post('order_id');
        $order = MerchantOrder::find()
            ->where(['id' => $id])
            ->one();

        // var_dump($id);exit;

        if ($order) {

            if ($order->status == MerchantOrder::$statusMapping['FINISH']) {
                return $this->fail('此订单已经核销过了');
            }

            $order->status = MerchantOrder::$statusMapping['FINISH'];
            $order->update_time = date('Y-m-d H:i:s');
            $order->save();

            return $this->success();
        }

        return $this->fail();
    }
}